/*************************************************************************
 * This file is part of HeroesForge.
 *
 * HeroesForge is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * HeroesForge is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with HeroesForge.  If not, see <http://www.gnu.org/licenses/>.
 *************************************************************************/
package org.diskordia.heroesforge.client.preferences;

import java.util.prefs.Preferences;

/**
 * Server connection settings container.
 * 
 * @author FSuite
 */
public class DefaultConnectionInfo implements IConnectionInfo {
	private final static String CONNECTION_URL = "Connection_Url";
	private final static String CONNECTION_USER = "Connection_User";
	private final static String CONNECTION_PASSWORD = "Connection_Password";
	private final static String CONNECTION_AUTOCONNECT = "Connection_Autoconnect";
	
	private Preferences preferences;
	
	/**
	 * Initializes an instance of the @see DefaultConnectionInfo class.
	 *
	 * @param prefs the preferences instance.
	 */
	public DefaultConnectionInfo(Preferences prefs)
	{
		if(prefs == null){
			throw new IllegalArgumentException("The prefs instance is a null reference.");
		}
		
		this.preferences = prefs;
	}
	
	/* (non-Javadoc)
	 * @see org.diskordia.heroesforge.client.preferences.IConnectionPreferences#getPassword()
	 */
	@Override
	public String getPassword() {		
		return this.preferences.get(CONNECTION_PASSWORD, "");
	}

	/* (non-Javadoc)
	 * @see org.diskordia.heroesforge.client.preferences.IConnectionPreferences#getServerUrl()
	 */
	@Override
	public String getServerUrl() {
		return this.preferences.get(CONNECTION_URL, "");
	}

	/* (non-Javadoc)
	 * @see org.diskordia.heroesforge.client.preferences.IConnectionPreferences#getUserName()
	 */
	@Override
	public String getUserName() {
		return this.preferences.get(CONNECTION_USER, "");
	}

	/* (non-Javadoc)
	 * @see org.diskordia.heroesforge.client.preferences.IConnectionPreferences#setPassword(java.lang.String)
	 */
	@Override
	public void setPassword(String password) {
		if(password == null) {
			throw new IllegalArgumentException("The password string is a null reference.");
		}
		
		this.preferences.put(CONNECTION_PASSWORD, password);
	}

	/* (non-Javadoc)
	 * @see org.diskordia.heroesforge.client.preferences.IConnectionPreferences#setServerUrl(java.lang.String)
	 */
	@Override
	public void setServerUrl(String url) {
		if(url == null) {
			throw new IllegalArgumentException("The url string is a null reference.");
		}
		
		this.preferences.put(CONNECTION_URL, url);
	}

	/* (non-Javadoc)
	 * @see org.diskordia.heroesforge.client.preferences.IConnectionPreferences#setUserName(java.lang.String)
	 */
	@Override
	public void setUserName(String user) {
		if(user == null) {
			throw new IllegalArgumentException("The user string is a null reference.");
		}
		
		this.preferences.put(CONNECTION_USER, user);
	}

	/* (non-Javadoc)
	 * @see org.diskordia.heroesforge.client.preferences.IConnectionPreferences#isAutoConnect()
	 */
	@Override
	public boolean isAutoConnect() {
		return this.preferences.getBoolean(CONNECTION_AUTOCONNECT, false);
	}

	/* (non-Javadoc)
	 * @see org.diskordia.heroesforge.client.preferences.IConnectionPreferences#setAutoConnect(boolean)
	 */
	@Override
	public void setAutoConnect(boolean enabled) {
		this.preferences.putBoolean(CONNECTION_AUTOCONNECT, enabled);
	}
}
